<?php
class Clan extends WotHub {

    private $_model_clan;
    public $clanId;
    
    public function afficherGarage() {
        $template = 'garageclan.tpl.php';
        $fonction = 'getGarageClan';
        $this->ajouterScript('js/garage.js');
        $params = $this->getParams() ;
        $this->showSkill = false;
        $clan = new Model_Clan($this->_pdo, $_SESSION['appId']);
        $this->filtres = $clan->getFiltresList();
        $membres = Model_Clan::getMembres($this->clanId);
        if (count($params) > 0) {
             if ($params[0] === 'Tiers') {
                 $template = 'garageclantiers.tpl.php';
             }
             if ($params[0] === 'Membre') {
                 $template = 'garageclanmembre.tpl.php';
                 $fonction = 'getGarageClanMembre';
                 $membres = Model_Clan::getMembresPseudo($this->clanId);
             }
        }
        $filtre = null;
        if (isset($_POST['tier']) === true) {
            $filtre = $_POST['tier'];
        }
        $this->_initData();
        $this->addTemplate($template);
        $this->_pdo = new smartbokModele($this->bdd);
        $this->clanInfo = $_SESSION['clanInfo'];
        $garage = new Model_Garage($this->_pdo, $_SESSION['appId']);
        $listeTanks = $garage->$fonction($membres, $filtre);
        
        if ($fonction === 'getGarageClanMembre') {
            $this->showSkill = true;
            $this->ajouterScript('js/garage.js');
            foreach ($listeTanks as &$membre) {
                foreach ($membre as &$tank) {
                    $tankId = $tank['tank_id'];
                    $accountId = $tank['account_id'];
                    $tankActif = new Model_Tank($this->_pdo, $_SESSION['appId']);
                    $tank['crew'] = $tankActif->getCrew($tankId);
                    $tank['skills'] = $tankActif->getCrewSkills($tankId, $accountId);
                    $tank['score'] = $tankActif->getScore($tankId, $accountId);
                }
            }
        }
        $this->tanks = $listeTanks;
    }
    
    public function Statistiques() {
        function insensitive_uksort($a, $b) {
            return strtolower($a)>strtolower($b);
        }
        $this->ajouterScript('js/clanstat.js');
        $this->addTemplate('clanstats.tpl.php');
        $lstmembres = implode(',',Model_Clan::getMembres($_SESSION['accountInfo']['clanId']));
        $clan = new Model_Clan($this->pdo, $_SESSION['appId']);
        $clanMemberTankStats = $clan->getClanMembersTanks($lstmembres);
        
        $_membresStats = $clan->getclanMembersStats($lstmembres);
        $_membresTiers = Ratings::getAvgTier($lstmembres);
        foreach ($_membresStats as $key => $membre) {
            $wn8data = null;
            $wr = $membre['statistics']['all']['wins']/$membre['statistics']['all']['battles']*100;
            $tmpMembres[$membre['nickname']]['stats'] = $membre['statistics']['all'];
            $tmpMembres[$membre['nickname']]['accountid'] = $key;
            $tmpMembres[$membre['nickname']]['stats']['tiers'] = $_membresTiers[$key];
            $tmpMembres[$membre['nickname']]['stats']['winRate'] = $wr;
            $tmpMembres[$membre['nickname']]['ER'] = Ratings::calculerER($tmpMembres[$membre['nickname']]['stats']);
            $tmpMembres[$membre['nickname']]['ER_status'] = Ratings::getRatingRang('ER', $tmpMembres[$membre['nickname']]['ER']);
            $tmpMembres[$membre['nickname']]['WN7'] = Ratings::calculerWN7($tmpMembres[$membre['nickname']]['stats']);
            $tmpMembres[$membre['nickname']]['WN7_status'] = Ratings::getRatingRang('WN7', $tmpMembres[$membre['nickname']]['WN7']);
            $wn8data[$key] = $clanMemberTankStats[$key];
            $tmpMembres[$membre['nickname']]['WN8'] = Ratings::calculerWN8($tmpMembres[$membre['nickname']]['stats'], $key, $wn8data);
            $tmpMembres[$membre['nickname']]['WN8_status'] = Ratings::getRatingRang('WN8', $tmpMembres[$membre['nickname']]['WN8']);
        }
        uksort($tmpMembres, 'insensitive_uksort');
        $this->membresStats = $tmpMembres;
    }
    public function StatMembres() {
        $params = explode('-', $this->getParams()[0]);
        $accountId = $params[0];
        $this->nickName =  $params[1];
        $this->addTemplate('clanmemberstat.tpl.php');
        $membre = new Model_Membre($this->_pdo,$this->appId);
        $tanks = $membre->getListeTank($accountId);
        $this->dataTanks = Model_Tank::getAllDataTank();
        $expTankValues = Ratings::getExpValues();
        
        foreach ($tanks as &$tank) {
            $tank['all']['tiers'] = $this->dataTanks[$tank['tank_id']]['level'];

            $tank['all']['winRate'] = 0;
            $tank['name'] = $this->dataTanks[$tank['tank_id']]['name'];
    
            if (isset($expTankValues[$tank['tank_id']]) === true) {
                $tank['exp'] = Ratings::setTankExpStat($expTankValues[$tank['tank_id']], $tank);
                $tank['WN8'] = Ratings::calculerTankWN8($expTankValues[$tank['tank_id']],$tank['all']);
                $tank['WN8_status'] = Ratings::getRatingRang('WN8', $tank['WN8']);
            }
            else {
                $tank['WN8'] = '--';
                $tank['exp'] = $tank['all'];
                $tank['WN8_status'] = '';
            }
            $tank['ER'] = Ratings::calculerER($tank['all']);
            $tank['ER_status'] = Ratings::getRatingRang('ER', $tank['ER']);
            $tank['WN7'] = Ratings::calculerWN7($tank['all']);
            $tank['WN7_status'] = Ratings::getRatingRang('WN7', $tank['WN7']);
        }
        foreach ($tanks as $tank) {
            if ($tank['all']['battles'] > 0 && $tank['in_garage'] === true) {
                $lstTanks[] = $tank;
            }
        }
        $this->listeTanks = $lstTanks;
    }
    
    public function Parametres() {
        $this->addTemplate('parametres.tpl.php');
        $this->ajouterScript('js/params.js');
        $this->_initData();
        $clanId = $_SESSION['clanInfo']['clan_id'];
        $clan = new Model_Clan($this->_pdo, $_SESSION['appId']);
        if (count($_POST) > 0) {
            $clan->insererParams($clanId); 
        }
        $this->clanParams = $clan->getClanParam($clanId);
        $this->strateurs = $clan->getStrateurs($clanId);
        $this->membres = array_diff(Model_Clan::getMembresPseudo($clanId), $this->strateurs);
    }
    
    public function Visuel() {
        $this->addTemplate('visuel.tpl.php');
        $this->ajouterScript('js/visuel.js');
        $this->_initData();
        if (isset($_POST['frm_Css']) === true) {
            $clan = new Model_Clan($this->_pdo, $_SESSION['appId']);
            $clan->enregisterCss($_SESSION['clanInfo']['abbreviation']);
        }
    }
    
    public function Accueil() {
        $this->addTemplate('msgaccueil.tpl.php');
        $this->msgAccueil = '';
        if (isset($_POST['frm_accueil']) === true) {
            $this->msgAccueil = $_POST['frm_accueil'];
            $clan = new Model_Clan($this->_pdo, $_SESSION['appId']);
            $clan->setMessageAccueil();
        }
    }
    
    public function GetTankMemberList() {
        $this->traiterAffichage = false;
        $this->_pdo = new smartbokModele($this->bdd);
        $tankId = $this->params[0];
        $this->clanInfo = $_SESSION['clanInfo'];
        $clan = new Model_Clan($this->_pdo, $_SESSION['appId']);
        $membres = $clan->getMembresPseudo($_SESSION['accountInfo']['clanId']);
        $lstMembre = $clan->getTankMembresListe(implode(',',array_keys($membres)));
        header('Content-Type: text/xml');
        echo "<?xml version=\"1.0\"?>\n";
        echo "<membres>\n";
        foreach($lstMembre as $membre) {
            echo '<donnee><tank>'.$membre['tank_id'].'</tank><account>'.$membre['account_id'].'</account><pseudo>'.$membres[$membre['account_id']].'</pseudo></donnee>';
        }
        echo "</membres>\n";
        $this->traiterAffichage = false;
        return true;
    }
    
    public function Mods() {
        $this->titre = 'Mods conseillés';
        $this->fieldSet1 = 'Mods préconisé par le clan';
        $this->fieldSet2  = 'Ajouter un mod';
        $this->_addRessources(Model_Clan::TYPE_MOD);
    }
    
    public function Liens() {
        $this->titre = 'Liens conseillés';
        $this->fieldSet1 = 'Liens intéressants pour le clan';
        $this->fieldSet2  = 'Ajouter un lien';
        $this->_addRessources(Model_Clan::TYPE_LIEN);
    }
    
    public function deleteRessource() {
        $this->_pdo = new smartbokModele($this->bdd);
        $modId = $this->params[0];
        $clan = new Model_Clan($this->_pdo, $_SESSION['appId']);
        $clan->deleteMod($modId);
        header('Content-Type: text/xml');
        echo "<?xml version=\"1.0\"?>\n";
        echo "<donnees>\n";
        echo "<id>".$modId."</id>";
        echo "</donnees>\n";
        $this->traiterAffichage = false;
        return true;
    }
    
    public function deleteFilter() {
        $this->_pdo = new smartbokModele($this->bdd);
        $modId = $this->params[0];
        $clan = new Model_Clan($this->_pdo, $_SESSION['appId']);
        $clan->deleteFiltre($modId);
        header('Content-Type: text/xml');
        echo "<?xml version=\"1.0\"?>\n";
        echo "<donnees>\n";
        echo "<id>".$modId."</id>";
        echo "</donnees>\n";
        $this->traiterAffichage = false;
        return true;
    }
    
    private function _addRessources($type) {
        $this->addTemplate('mods.tpl.php');
        $this->ajouterScript('js/liens.js');
        $clan = new Model_Clan($this->_pdo, $_SESSION['appId']);
        if (count($_POST) > 0) {
            if ($_POST['frm_nom'] !== '' && $_POST['frm_description'] !== '' && $_POST['frm_lien'] !== '') {
               $clan->enregistrerMod($type, $_POST['frm_id']);
            }
            else {
                SmartbokMessage::addMessage(SmartbokMessage::ERR, 'Veuillez saisir tous les champs.');
            }
        }
        $this->mods = $clan->getModsList($type);
    }
    
    public function Filtres() {
        $this->titre = 'Filtres';
        $this->addTemplate('filtres.tpl.php');
        $this->fieldSet1 = 'Filtres personnalisés';
        $this->fieldSet2  = 'Ajouter un filtre';
        $this->ajouterScript('js/filtres.js');
        $clan = new Model_Clan($this->_pdo, $_SESSION['appId']);
        $events = Model_Agenda::getListeEventType(); 
        array_pop($events);
        $this->events = $events;
        $this->tanks = json_encode(Model_Tank::getAllDataTank());
        if (count($_POST) > 0) {
            if ($_POST['frm_nom'] !== '' && $_POST['frm_categorie'] !== '' && $_POST['frm_tanks'] !== '') {
                $clan->enregistrerFiltre($_POST['frm_id']);
            }
            else {
                SmartbokMessage::addMessage(SmartbokMessage::ERR, 'Veuillez saisir tous les champs.');
            }
        }
        $this->filtres = $clan->getFiltresList(true);
    }
}

/*stats multiple : 
http://api.worldoftanks.ru/2.0/account/info/?application_id=171745d21f7f98fd8878771da1000a31&account_id=19213,461*/